package com.yanqu.road.server.manger.wingroom;

import com.yanqu.road.dao.impl.wingroom.WingRoomConfigDaoImpl;
import com.yanqu.road.dao.impl.wingroom.chakra.WingRoomChakraConfigDaoImpl;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eCricketQualityType;
import com.yanqu.road.entity.wingroom.chakra.config.WingRoomChakraAttributeConfig;
import com.yanqu.road.entity.wingroom.chakra.config.WingRoomChakraChartConfig;
import com.yanqu.road.entity.wingroom.chakra.config.WingRoomChakraConfig;
import com.yanqu.road.entity.wingroom.chakra.config.WingRoomChakraWindWaterConfig;
import com.yanqu.road.entity.wingroom.config.WingRoomFurnitureConfig;
import com.yanqu.road.entity.wingroom.config.WingRoomLvConfig;
import com.yanqu.road.entity.wingroom.config.WingRoomRegionConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2022/1/21 16:34
 */
public class WingRoomConfigMgr extends TempMgr {
    private static Map<Integer, WingRoomFurnitureConfig> furnitureConfigMap = new HashMap<>();
    private static Map<Integer, List<WingRoomFurnitureConfig>> suitConfigMap = new HashMap<>();
    private static Map<Integer, WingRoomLvConfig> wingRoomLvConfigList = new HashMap<>();

    //命盘详情
    private static Map<Integer, Map<Integer, WingRoomChakraConfig>> wingRoomChakraConfigMap = new ConcurrentHashMap<>();
    //命盘风水
    private static Map<Integer, WingRoomChakraWindWaterConfig> wingRoomChakraWindWaterConfigMap = new ConcurrentHashMap<>();
    private static List<WingRoomChakraWindWaterConfig> wingRoomChakraWindWaterConfigList = new ArrayList<>();
    //命盘等级
    private static Map<Integer, WingRoomChakraChartConfig> wingRoomChakraChartConfigMap = new ConcurrentHashMap<>();
    //K:命盘等级,V:到下一级总经验
    private static Map<Integer, Long> wingRoomChakraChartLevelExpMap = new ConcurrentHashMap<>();
    //命盘属性
    private static Map<Integer, Map<Integer, WingRoomChakraAttributeConfig>> wingRoomChakraAttributeConfigMap = new ConcurrentHashMap<>();

    public static Map<Integer, WingRoomFurnitureConfig> getFurnitureConfigMap() {
        return furnitureConfigMap;
    }

    public static Map<Integer, List<WingRoomFurnitureConfig>> getSuitConfigMap() {
        return suitConfigMap;
    }

    public static WingRoomFurnitureConfig getFurnitureConfig(int furnitureId) {
        return furnitureConfigMap.get(furnitureId);
    }

    public static WingRoomRegionConfig getRegionConfig(int regionId) {
        return ConfigMgr.getRegionConfigMap().get(regionId);
    }

    public static WingRoomLvConfig getWingRoomLvConfig(int lv) {
        return wingRoomLvConfigList.get(lv);
    }

    public static boolean checkRegionUnlock(int regionId, int lv) {
        for (int i = 1; i <= lv; i++) {
            WingRoomLvConfig wingRoomLvConfig = wingRoomLvConfigList.get(i);
            if (wingRoomLvConfig == null) {
                break;
            }
            if (wingRoomLvConfig.getUnlockRegion().contains(regionId)) {
                return true;
            }
        }
        return false;
    }

    public static Map<Integer, Map<Integer, WingRoomChakraConfig>> getWingRoomChakraConfigMap() {
        return wingRoomChakraConfigMap;
    }

    public static Map<Integer, WingRoomChakraChartConfig> getWingRoomChakraChartConfigMap() {
        return wingRoomChakraChartConfigMap;
    }

    public static Map<Integer, Map<Integer, WingRoomChakraAttributeConfig>> getWingRoomChakraAttributeConfigMap() {
        return wingRoomChakraAttributeConfigMap;
    }

    /**
     * 获取盘位配置
     * @param occupation
     * @param sort
     * @return
     */
    public static WingRoomChakraConfig getWingRoomChakraConfig(int occupation, int sort) {
        Map<Integer, WingRoomChakraConfig> sortMap = wingRoomChakraConfigMap.get(occupation);
        if (sortMap != null) {
            return sortMap.get(sort);
        }
        return null;
    }

    /**
     * 获取命盘等级配置
     * @param chakraLevel
     * @return
     */
    public static WingRoomChakraChartConfig getWingRoomChakraChartConfig(int chakraLevel) {
        return wingRoomChakraChartConfigMap.get(chakraLevel);
    }

    /**
     * 获取到下一个命盘等级，需要的总经验
     * @param level
     * @return
     */
    public static Long getChakraNextLevelNeedTotalExp(int level) {
        return wingRoomChakraChartLevelExpMap.get(level);
    }

    /**
     * 获取命盘属性配置
     * @param quality 盘位品质
     * @param level 盘位等级
     * @return
     */
    public static WingRoomChakraAttributeConfig getWingRoomChakraAttributeConfig(int quality, int level) {
        Map<Integer, WingRoomChakraAttributeConfig> levelMap = wingRoomChakraAttributeConfigMap.get(quality);
        if (levelMap != null) {
            return levelMap.get(level);
        }
        return null;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        furnitureConfigMap = new WingRoomConfigDaoImpl().getFurnitureConfigMap();
        Map<Integer, List<WingRoomFurnitureConfig>> tempSuitConfigMap = new ConcurrentHashMap<>();
        for (WingRoomFurnitureConfig furnitureConfig : furnitureConfigMap.values()) {
            if (!tempSuitConfigMap.containsKey(furnitureConfig.getSuitType())) {
                tempSuitConfigMap.put(furnitureConfig.getSuitType(), new ArrayList<>());
            }
            tempSuitConfigMap.get(furnitureConfig.getSuitType()).add(furnitureConfig);
        }
        suitConfigMap = tempSuitConfigMap;

        wingRoomLvConfigList = new WingRoomConfigDaoImpl().getWingRoomLvConfigList();

        //读取命盘配置进内存
        WingRoomChakraConfigDaoImpl chakraConfigDao = new WingRoomChakraConfigDaoImpl();
        //命盘详情
        wingRoomChakraConfigMap = chakraConfigDao.getWingRoomChakraConfigMap();
        //命盘风水
        wingRoomChakraWindWaterConfigMap = chakraConfigDao.getWingRoomWindChakraWaterConfigMap();
        wingRoomChakraWindWaterConfigList = chakraConfigDao.getWingRoomWindChakraWaterConfigList();
        //命盘等级
        wingRoomChakraChartConfigMap = chakraConfigDao.getWingRoomChakraChartConfigMap();
        //命盘等级-总经验映射MAP。（可减少升级遍历表次数）
        wingRoomChakraChartLevelExpMap.clear();
        List<WingRoomChakraChartConfig> wingRoomChakraChartConfigList = new ArrayList<>(wingRoomChakraChartConfigMap.values());
        wingRoomChakraChartConfigList.sort(Comparator.comparing(WingRoomChakraChartConfig::getId));
        long levelNeedExp = 0;
        for (WingRoomChakraChartConfig chakraChartConfig : wingRoomChakraChartConfigList) {
            if (chakraChartConfig.getExp() > 0) {
                levelNeedExp += chakraChartConfig.getExp();
                wingRoomChakraChartLevelExpMap.put(chakraChartConfig.getId(), levelNeedExp);
            }
        }
        //命盘属性
        wingRoomChakraAttributeConfigMap = chakraConfigDao.getWingRoomChakraAttributeConfigMap();

        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    /**
     * 获取风水等级对应的风水配置
     * @param windWaterLevel
     * @return
     */
    public static WingRoomChakraWindWaterConfig getChakraWindWaterConfig(int windWaterLevel) {
        return wingRoomChakraWindWaterConfigMap.get(windWaterLevel);
    }

    /**
     * 获取风水配置LIST
     * @return
     */
    public static List<WingRoomChakraWindWaterConfig> getWingRoomChakraWindWaterConfigList() {
        return wingRoomChakraWindWaterConfigList;
    }

    /**
     * 获取家具升级/解锁奖励
     * @param furnitureId
     * @return
     */
    public static Map<Integer, String> getFurnitureUpgradeRewardMap(int furnitureId) {
        Map<Integer, String> map = new LinkedHashMap<>();
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(furnitureId);
        if (goodsInfo == null) {
            return map;
        }
        String config = null;
        if (goodsInfo.getQuality() == eCricketQualityType.Green.getValue()) {
            config = GameConfig.CHAKRAS_FURNITURE_REWARD_1;
        } else if (goodsInfo.getQuality() == eCricketQualityType.Blue.getValue()) {
            config = GameConfig.CHAKRAS_FURNITURE_REWARD_2;
        } else if (goodsInfo.getQuality() == eCricketQualityType.Purple.getValue()) {
            config = GameConfig.CHAKRAS_FURNITURE_REWARD_3;
        } else if (goodsInfo.getQuality() == eCricketQualityType.Orange.getValue()) {
            config = GameConfig.CHAKRAS_FURNITURE_REWARD_4;
        } else if (goodsInfo.getQuality() == eCricketQualityType.Red.getValue()) {
            config = GameConfig.CHAKRAS_FURNITURE_REWARD_5;
        }
        if (config == null) {
            return map;
        }
        List<String> list = StringUtils.stringToStringList(config, "\\|");
        for (String levelRewardStr : list) {
            String[] levelRewardArr = levelRewardStr.split(",");
            int level = Integer.parseInt(levelRewardArr[0]);
            String rewardStr = levelRewardArr[1];
            map.put(level, rewardStr);
        }
        return map;
    }

    /**
     * 获取风水配置根据总风水值
     * @param windWaterValue
     * @return
     */
    public static WingRoomChakraWindWaterConfig getWingRoomChakraWindWaterConfigByTotalWindWater(long windWaterValue) {
        //获取命中的配置
        WingRoomChakraWindWaterConfig wingRoomChakraWindWaterConfig = null;
        for (WingRoomChakraWindWaterConfig config : wingRoomChakraWindWaterConfigList) {
            if (windWaterValue >= config.getGeomancyId()) {
                wingRoomChakraWindWaterConfig = config;
            } else {
                break;
            }
        }
        return wingRoomChakraWindWaterConfig;
    }

    /**
     * 获取词条数量（门客数量）
     * @param windWaterValue
     * @return
     */
    public static int getDrawPatronsNum(long windWaterValue) {
        //获取命中的配置
        WingRoomChakraWindWaterConfig wingRoomChakraWindWaterConfig = getWingRoomChakraWindWaterConfigByTotalWindWater(windWaterValue);
        //基础数
        int baseNum = GameConfig.CHAKRAS_ENTRY_NUMBER_PARAM <= 0 ? 1 : GameConfig.CHAKRAS_ENTRY_NUMBER_PARAM;
        //获取倍数
        int mulNum = 1;
        if (wingRoomChakraWindWaterConfig != null) {
            mulNum = RandomHelper.getRandomKeyByWeight(wingRoomChakraWindWaterConfig.getEntryWeight(), "\\|", ",");
        }
        return mulNum * baseNum;
    }
}
